//
//  BridgeScrollViewApp.swift
//  BridgeScrollView
//
//  Created by zjkj on 2024/8/2.
//

import SwiftUI

@main
struct BridgeScrollViewApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView1()
        }
    }
}


struct ContentView1: View {
    
    @ObservedObject var configuration = BridgeScrollConfiguration()
    
    @State var subViews: [Int] = []
    
    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    
    var body: some View {
        ScrollView(.vertical) {
            VStack {
                ForEach(subViews, id: \.self) { idx in
                    Button(action: {
                        withAnimation {
                            configuration.isBounces.toggle()
                        }
                        
                    }, label: {
                        Text("\(idx)")
                            .frame(width: 200, height: 50)
                            .background(.red)
                            .id("\(idx)")
                    })
                }
            }
        }
        .onReceive(timer, perform: { _ in
            subViews.append(Int(arc4random()) % 50)
        })
        .scrollViewOffsetObserver(configuration)
        .onReceive(configuration.$contentOffset) { value in
            print("contentOffset", value)
        }
        .onReceive(configuration.$contentSize) { value in
            print("contentSize", value)
        }
        .onReceive(configuration.$isScrolling) { value in
            print("isScrolling", value)
        }
        .onReceive(configuration.$isDecelerating) { value in
            print("isDecelerating", value)
        }
        .onReceive(configuration.$isDragging) { value in
            print("isDragging", value)
        }
        .onReceive(configuration.$isTop) { value in
            print("isTop", value)
        }
        .onReceive(configuration.$isBottom) { value in
            print("isBottom", value)
        }
        .onReceive(configuration.$isZooming) { value in
            print("isZooming", value)
        }
        .onReceive(configuration.$zoomLevel) { value in
            print("zoomLevel", value)
        }
        .onReceive(configuration.$isUserScroll) { value in
            print("isUserScroll", value)
        }
        .onReceive(configuration.$scrollType) { value in
            print("scrollType", value)
        }
        .onReceive(configuration.$isFull) { value in
            print("isFull", value)
        }
        
    }
}

#Preview(body: {
    ContentView1()
})
